package com.hb.trade.server.center.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.center.sys.dto.CascadeQueryDTO;
import com.hb.core.pojo.center.sys.entity.DicCode;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.center.sys.service.DicCodeService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 * 数据字典信息 前端控制器
 *
 * @author zhaojk
 * @since 2021-05-21
 */
@RestController
public class DicCodeController extends MPBaseController<DicCode, DicCodeService> {

    public DicCodeController(DicCodeService service) {
        super(service);
    }

    @PostMapping("/dicCode/pageDicCodes")
    public Message<PageResult<DicCode>> pageDicCodes(@RequestBody DicCode dicCode) {
        Page<DicCode> dicCodePage = dicCode.pages();
        QueryWrapper<DicCode> wrapper = DicCode.wrapper();
        wrapper.eq(dicCode.getTypeId() != null, "type_id", dicCode.getTypeId());
        wrapper.orderByAsc(CollUtil.list(false, "type_id", "dic_key"));
        page(wrapper, dicCodePage);
        return MsgUtil.success(PageResult.get(dicCodePage));
    }

    /**
     * 获取指定类型的字典数据
     * 若字典类型为空, 则获取所有
     *
     * @param dicCode
     * @return
     */
    @PostMapping("/dicCode/listDicCodes")
    public Message<List<DicCode>> listDicCodes(@RequestBody DicCode dicCode) {
        QueryWrapper<DicCode> wrapper = DicCode.wrapper();
        wrapper.eq(dicCode.getTypeId() != null, "type_id", dicCode.getTypeId())
                .eq(dicCode.getParentId() != null, "parent_id", dicCode.getParentId());
        wrapper.orderByAsc(CollUtil.list(false, "type_id", "sort"));
        List<DicCode> dicCodes = service.list(wrapper);
        return MsgUtil.success(dicCodes);
    }

    /**
     * 根据key值或label值查询字典数据
     *
     * @param keyOrLabel
     * @return
     */
    @PostMapping("/dicCode/findDicData")
    public Message<DicCode> findDicData(Serializable keyOrLabel) {
        if (keyOrLabel == null) {
            return MsgUtil.msg(MsgEnum.NULL_RESULT);
        }
        QueryWrapper<DicCode> wrapper = DicCode.wrapper();
        wrapper.eq("dic_key", keyOrLabel).or().eq("dic_label", keyOrLabel);
        DicCode dicCode = service.getOne(wrapper);
        return MsgUtil.success(dicCode);
    }

    @PostMapping("/dicCode/updateDicCode")
    public Message<Boolean> updateDicCode(@RequestBody DicCode dicCode) {
        boolean saveOrUpdate = saveOrUpdate(dicCode);
        return MsgUtil.success(saveOrUpdate);
    }

    @PostMapping("/dicCode/deleteDicCodes")
    public Message<Boolean> deleteDicCodes(@RequestBody List<Long> ids) {
        UpdateWrapper<DicCode> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("delete_flag", 1)
                .set("update_time", DateUtil.date())
                .in("id", ids);
        boolean result = service.update(updateWrapper);
        return MsgUtil.success(result);
    }

    /**
     * 查询指定表的最顶级数据
     *
     * @param topQuery
     * @return
     * @deprecated 方法逻辑有错误
     */
    @Deprecated
    @PostMapping("/common/getTopData")
    public Message<JSONObject> getTopData(@RequestBody CascadeQueryDTO topQuery) {
        JSONObject data = service.getTopData(topQuery);
        return MsgUtil.success(data);
    }

    /**
     * 查询指定表的所有子集数据
     *
     * @param topQuery
     * @return
     */
    @PostMapping("/common/getChildList")
    public Message<List<Long>> getChildList(@RequestBody CascadeQueryDTO topQuery) {
        List<Long> datas = service.getChildList(topQuery);
        return MsgUtil.success(datas);
    }

    @PostMapping("/common/getByPrimary")
    public Message<DicCode> getByPrimary(Long dicCodeId) {
        DicCode byId = getById(dicCodeId);
        if (byId == null) {
            return MsgUtil.msg(MsgEnum.NULL_RESULT);
        }
        return MsgUtil.success(byId);
    }


    /**
     * 根据类型代码和值查询字典名称
     * @author tian
     * @date 2022-3-1 17:29
     * @param typeCode
     * @param dicLabel
     * @return org.fzzn.core.model.msg.Message<com.hb.core.pojo.center.sys.entity.DicCode>
     */
    @PostMapping("/dicCode/selectDicCodeByTypeCodeAndDicLabel")
    public Message<DicCode> selectDicCodeByTypeCodeAndDicLabel(@RequestParam("typeCode")String typeCode, @RequestParam("dicLabel")String dicLabel) {
        DicCode dicCode = service.selectDicCodeByTypeCodeAndDicLabel(typeCode, dicLabel);
        return MsgUtil.success(dicCode);
    }
}
